package cn.sinata.xldutils_kotlin.rxutils

import cn.sinata.xldutils_kotlin.activity.BaseActivity
import cn.sinata.xldutils_kotlin.activity.RecyclerActivity
import cn.sinata.xldutils_kotlin.application.BaseApplication
import cn.sinata.xldutils_kotlin.data.ResultData
import cn.sinata.xldutils_kotlin.fragment.BaseFragment
import cn.sinata.xldutils_kotlin.fragment.RecyclerFragment
import cn.sinata.xldutils_kotlin.sysErr
import com.google.gson.JsonSyntaxException
import org.jetbrains.anko.support.v4.toast
import org.jetbrains.anko.toast
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException

/**
 * 服务器返回数据基本处理
 */
abstract class ResultDataSubscriber<T> : ResultSubscriber<ResultData<T>> {
    constructor(activity: BaseActivity) : super(activity)
    constructor(fragment: BaseFragment) : super(fragment)

    override fun onNext(t: ResultData<T>) {
        sysErr(t.msg+"---------"+t.data+"-----"+t.code)
        SystemUtil.sysTime = t.sys!!
        dismissDialog()
        if (t.code == 0) {
            if (t.msg == null) {
                t.msg=""
            }
            onSuccess(t.msg,t.data)
        } else {
            onError(ResultException(t.code, t.msg))
        }
    }

    /**
     * 默认错误方法。
     */
    override fun onError(t: Throwable) {
        super.onError(t)
        sysErr(t.message)
        dismissDialog()
        t.printStackTrace()
        var code = -1
        var msg = Error.REQUEST_ERROR
        when (t) {
            is JsonSyntaxException -> msg = Error.PARSER_ERROR
            is ConnectException -> msg = Error.NET_ERROR
            is SocketTimeoutException -> msg = Error.NET_ERROR
            is HttpException -> msg = Error.SERVER_ERROR
            is ResultException -> {
                code = t.code
                msg = t.message!!
            }

        }
        onError(code, msg)
    }

    /**
     * 错误时处理。如有特殊code判断，复写此方法
     */
    open fun onError(code: Int, msg: String) {
        if (isShowToast()) {
            _showToast(msg)
        }
    }

    private fun _showToast(msg: String) {
        if (activities != null && activities?.get() != null) {
            val activity = (activities?.get() as BaseActivity)
            activity.toast(msg)
        }
        if (fragments != null && fragments?.get() != null) {
            val fragment = (fragments?.get() as BaseFragment)
            fragment.toast(msg)
        }
    }

    private fun dismissDialog() {
        if (activities != null && activities?.get() != null) {
            val activity = (activities?.get() as BaseActivity)
            activity.dismissDialog()
            if (activity is RecyclerActivity) {
                activity.setRefresh(false)
            }
        }
        if (fragments != null && fragments?.get() != null) {
            val fragment = (fragments?.get() as BaseFragment)
            fragment.dismissDialog()
            if (fragment is RecyclerFragment) {
                fragment.setRefreshing(false)
            }
        }
    }

    /**
     * 默认展示错误Toast
     */
    open fun isShowToast(): Boolean = true

    /**
     * 服务器返回成功code时数据
     */
    abstract fun onSuccess(msg: String?, data: T?)
}